package com.superview.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.Session;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import steed.action.BaseAction;
import steed.hibernatemaster.domain.BaseDatabaseDomain;
import steed.util.base.BaseUtil;

@Namespace("/speak")
@ParentPackage(value = "steed")
@InterceptorRefs({ @InterceptorRef(value = "mydefault") })
public class SpeakAction extends BaseAction<BaseDatabaseDomain> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

//	@Action("index")
//	public String index() {
//		WebSockAction.wavRootPath = getRequest().getContextPath();
//		return steed_forward;
//	}

	@Action("speak")
	public String speak() {
		// 1.获取当前表情识别结果
		String emotion_text = getRequestParameter("emotion_text");
		System.out.println(emotion_text);
		// 2.获取表情结果对应的回复
		String response = getResponseByEmotion(emotion_text);
		// 4.websocket给前端发送信息，前端将语音下载下来然后播放声音
		SendMsg2Client(response);
		return null;
	}
	@Action("speak2")
	public String speak2() {
		String text = getRequestParameter("text");
		SendMsg2Client(text);
		writeObjectMessage("");
		return null;
	}
	@Action("speak3")
	public String speak3() throws IOException {
		String text = getRequestParameter("text");
		CopyOnWriteArrayList<Session> sessions = WebSockAction.sessions;
		for (Session item : sessions) {
//	        	if (item.equals(session)) {
//					continue;
//				}
			WebSockAction.sendMessage(item, "/GAME/xunirenanimation");

		}
		writeObjectMessage("");
		return null;
	}
	
	@Action("makeExpression")
	public String makeExpression() {
		// 1.获取当前表情识别结果
		String emotion_text = getRequestParameter("emotion_text");
		System.out.println(emotion_text);
		//根据表情id从数据库获取对应的表情资源路径，直接传送到前端驱动UE4播放动画
		String emotionUri = getEmotionUriById(emotion_text);
		// 4.websocket给前端发送信息，前端将语音下载下来然后播放声音
		SendEmotion2Client(emotionUri);
		return null;
	}
	@Action("wakeup")
	public String wakeup() {
		String emotionUri = "/GAME/blendshape/assest/wakeup";
		// 4.websocket给前端发送信息，前端将语音下载下来然后播放声音
		SendEmotion2Client(emotionUri);
		return null;
	}
	@Action("sleep")
	public String sleep() {
		String emotionUri = "/GAME/blendshape/assest/start_pose";
		// 4.websocket给前端发送信息，前端将语音下载下来然后播放声音
		SendEmotion2Client(emotionUri);
		return null;
	}
	private void SendEmotion2Client(String emotionUri) {
		CopyOnWriteArrayList<Session> sessions = WebSockAction.sessions;
		for (Session item : sessions) {
//	        	if (item.equals(session)) {
//					continue;
//				}
			WebSockAction.sendEmotion(item, emotionUri);

		}
	}
	//未来通过查询数据库获取uri
	private String getEmotionUriById(String emotion_text) {
		String emotionUri = "/GAME/blendshape/expressionAnim/emotion/neutral";
		switch (emotion_text) {
		//七种表情
		case "happy":
			emotionUri = "/GAME/blendshape/expressionAnim/emotion/happy";// 看起来很开心呀
			break;
		case "surprise":
			emotionUri = "/GAME/blendshape/expressionAnim/emotion/surprise";
			break;
		case "angry":
			emotionUri = "/GAME/blendshape/expressionAnim/emotion/angry";
			break;
		case "sad":
			emotionUri = "/GAME/blendshape/expressionAnim/emotion/sad";
			break;
		case "fear":
			emotionUri = "/GAME/blendshape/expressionAnim/emotion/fear";
			break;
		case "disgust":
			emotionUri = "/GAME/blendshape/expressionAnim/emotion/disgust";
			break;
		case "neutral":
			emotionUri = "/GAME/blendshape/expressionAnim/emotion/neutral";
			break;
		//其他的动作，比如唤醒，眨眼
		case "blink":
			emotionUri = "/GAME/blendshape/expressionAnim/blink";
			break;
		case "open_eyes":
			emotionUri = "/GAME/blendshape/expressionAnim/open_eyes";
			break;
		case "smile":
			emotionUri = "/GAME/blendshape/expressionAnim/smile";
			break;
		//肢体的动作
		case "jump":
			emotionUri = "/GAME/motion/jump";
			break;
		case "kick":
			emotionUri = "/GAME/motion/kick";
			break;
		case "punch":
			emotionUri = "/GAME/motion/punch";
			break;
		case "run":
			emotionUri = "/GAME/motion/run";
			break;
		case "sit":
			emotionUri = "/GAME/motion/sit";
			break;
		case "squat":
			emotionUri = "/GAME/motion/squat";
			break;
		case "stand1":
			emotionUri = "/GAME/motion/stand1";
			break;
		case "walk":
			emotionUri = "/GAME/motion/walk";
			break;
		case "wave":
			emotionUri = "/GAME/motion/wave";
			break;
		default:// 自然
			break;
		}
		return emotionUri;
	}
	@Action("imitaeMotion")
	public String imitaeMotion() throws IOException {
		String text = getRequestParameter("text");
		CopyOnWriteArrayList<Session> sessions = WebSockAction.sessions;
		for (Session item : sessions) {
//	        	if (item.equals(session)) {
//					continue;
//				}
			WebSockAction.sendMessage(item, "/GAME/xunirenanimation");

		}
		writeObjectMessage("");
		return null;
	}
	private void SendMsg2Client(String param) {
		CopyOnWriteArrayList<Session> sessions = WebSockAction.sessions;
		for (Session item : sessions) {
//	        	if (item.equals(session)) {
//					continue;
//				}
			WebSockAction.sendParams(item, param);

		}
	}


	private String getResponseByEmotion(String emotion_text) {
		String response = "您今天看起来气色不错！";
		switch (emotion_text) {
		case "happy":
			response = "您今天看起来很开心呀！";// 看起来很开心呀
			break;
		case "surprise":
			response = "我猜您一定是因为我长得这么好看而感到惊讶！";
			break;
		case "angry":
			response = "您怎么了？我是不是做错什么了？";
			break;
		case "sad":
			response = "您是不是遇到什么烦心事了？";
			break;
		default:// 自然
			break;
		}
//		return response;
		return "你好，我是超维视界Lucy";
	}

	private String filename;
	private InputStream inputStream;

	@Action(value = "downloadwav", results = { @Result(name = "success", type = "stream", params = { "contentType",
			"application/octet-stream", "inputName", "inputStream", "contentDisposition",
			"attachment;filename=\"${filename}\"", "bufferSize", "4096" }) })
	public String downloadwav() {
		setFilename("FCHENLI01.wav");
		HttpServletRequest request = ServletActionContext.getRequest();
		String path = request.getContextPath();
		BaseUtil.out(path + getFilename());
		File file = new File(path + getFilename());

		if (!file.exists()) {
			BaseUtil.out("fail");
			return null;
		}
		try {
			inputStream = new FileInputStream(new File(filename));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	@Action("downloadwav2")
	public String downloadwav2() {

		return null;
	}

}
